package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.dto.Reservation;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.ReservationService;

@Controller
public class ReservationController extends RestController {

    @Autowired
    ReservationService reservationService;
	
    private static final Logger log = Logger.getLogger(ReservationController.class.getName());
    
    @RequestMapping(value = "/reservations", method = RequestMethod.GET)
    public @ResponseBody List<Reservation> get() {
        return reservationService.getAll();         
    }
    
    @RequestMapping(value = "/reservations/history", method = RequestMethod.GET)
    public @ResponseBody List<Reservation> getHistory() {
        return reservationService.getAllHistory();
    }
    
    @RequestMapping(value = "/reservations/user/{id}", method = RequestMethod.GET)
    public @ResponseBody List<Reservation> getForUser(@PathVariable long id) {
        return reservationService.getForUser(id);         
    }
    
    @RequestMapping(value = "/reservations/history/user/{id}", method = RequestMethod.GET)
    public @ResponseBody List<Reservation> getHistoryForUser(@PathVariable long id) {
        return reservationService.getHistoryForUser(id);
    }
    
    @RequestMapping(value = "/reservation", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody Reservation reservation) {
        log.info("Create new reservation.");
    	boolean result = reservationService.create(reservation);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/reservation/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody Reservation reservation) {
        log.info("Update reservation with id " + id);
        boolean result = reservationService.update(id, reservation);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/reservation/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        boolean result = reservationService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
   
}
